/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-14 18:48:05
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-15 13:56:31
 */
import barrackCtr from "../ctrs/barracks/barrackCtr";
import barrackEntity from "../ctrs/barracks/barrackEntity";
import gameCtr from "../ctrs/gameCtr";
import treasureCtr from "../ctrs/treasure/treasureCtr";
import treasureEntity from "../ctrs/treasure/treasureEntity";
import treasureTp from "../ctrs/treasure/treasureTp";
import BaseGameComponent from "../libs/core/BaseGameComponent";

const { ccclass, property } = cc._decorator

@ccclass

export default class rewardItem extends BaseGameComponent {

  @property({ type: cc.Sprite, tooltip: "icon" })
  icon: cc.Sprite = null;

  @property({ type: cc.Label, tooltip: "名称" })
  nameTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "数量" })
  numTxt: cc.Label = null;

  public onEnable(): void {
    super.onEnable();
    this.node.y = 0;
    this.node.x = 0;
    this.node.opacity = 255;
  }

  drawUi(data) {
    console.log(data)

    let that = this;
    switch (data.type) {
      case 1:
        cc.resources.load("ele/diamond", cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
          if (err) return;
          that.icon.spriteFrame = sp;
          that.nameTxt.string = "钻石";
          that.numTxt.string = `+${data.num}`;
        });
        break;
      case 2:
        let barrackEny: barrackEntity = barrackCtr.ins.getBarrackById(data.id);
        cc.resources.load(barrackEny.iconUrl, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
          if (err) return;
          that.icon.spriteFrame = sp;
          that.nameTxt.string = `${barrackEny.name}碎片`;
          that.numTxt.string = `+${data.num}`;
        });
        break;
      case 3:
        let treasureT: treasureTp = treasureCtr.ins.treasureTpDic.get(data.id);
        cc.resources.load(treasureT.iconUrl, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
          if (err) return;
          that.icon.spriteFrame = sp;
          that.nameTxt.string = `${treasureT.name}`;
          that.numTxt.string = `+${data.num}`;
        });
        break;
    }

    cc.tween(this.node)
      .to(2, { y: 300, opacity: 150 })
      .call(() => {
        gameCtr.ins.poolObj['rewardItem'].put(that.node);
      })
      .start();
  }
}